package cc.lzmsh.controller;

import cc.lzmsh.domain.entity.Article;
import cc.lzmsh.domain.result.Result;
import cc.lzmsh.domain.vo.ArticleDetailVo;
import cc.lzmsh.domain.vo.ArticleListVo;
import cc.lzmsh.domain.vo.HotArticleVo;
import cc.lzmsh.domain.vo.PageVo;
import cc.lzmsh.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author:   msh
 * Date:     2024/9/13 下午3:10
 * Description:
 */

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/hotArticleList")
    public Result<List<HotArticleVo>> hotArticleList() {
        // 查询点击量最多的前10篇文章
        return articleService.hotArticleList();
    }

    /**
     * 文章列表
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @return
     */
    @GetMapping("/articleList")
    public PageVo<ArticleListVo> getArticleList(Integer pageNum, Integer pageSize, Long categoryId) {
        List<ArticleListVo> articleList = articleService.getArticleList(pageNum, pageSize, categoryId);
        PageVo<ArticleListVo> articleListVoPageVo = new PageVo<>();
        articleListVoPageVo.setTotal(articleList.size());
        articleListVoPageVo.setRecords(articleList);
        return articleListVoPageVo;
    }

    @GetMapping("/{id}")
    public Result<ArticleDetailVo> getArticleDetail(@PathVariable("id") Long id) {
        ArticleDetailVo articleDetailVo = articleService.getArticleDetail(id);
        return Result.success(articleDetailVo);
    }

    @PutMapping("/updateViewCount")
    public Result updateViewCount(@RequestParam("id") Long id) {
        return articleService.updateViewCount(id);
    }
}
